home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Trading on the Edge
/
Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin
/
pc
/
shared
/
azte_pro
/
intro.c
< prev
next >
Wrap
Text File
|
1993-06-11
|
6KB
|
279 lines
/*
* intro.c
* Converted for hi by Sean Coates, November 1992.
*/
#include "define.h"
#ifndef NO_STDLIB_H
#include <stdlib.h>
#endif
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#ifndef NO_MALLOC_H
#include <malloc.h>
#endif
#define TRUE 1
#define FALSE 0
/* constants */
#define DATA_FILE "tokens.out" /* file to read token data from */
#define LOG_FILE "log.out"
#define MAXTOKENS 4 /* max allowable for each buyer/seller */
#define MAXTRIES 2000 /* in data file - must match MAXTOKENS */
/* in humgraph.c and maketoks.c */
#define MAXPLAYERS 8
/* externals */
void open_intro_data();
void intro_chart();
void show_supply_demand();
int readint();
int gettok();
/* global variables - besides the general variables all players know about */
FILE *fp, *logfil; /* global file pointer holds data file */
extern int gametype;
int ntries, ntoks;
int intro_role;
int pass=FALSE;
int confirm=TRUE;
int randomized_confirm_keys = TRUE;
int allow_losses = FALSE;
typedef struct {
int round;
int period;
int step;
char offers[35];
char bids[35];
char price[10];
} history_rec;
history_rec *history_data;
int nbuyers, nsellers;
/*
*************************************************************************
** show_supply_demand
** This function loops though the same token file and
** displays supply and demand as tables for each set of tokens.
*************************************************************************
*/
void show_supply_demand(fp, gametype, ntoks, nbuyers, nsellers, ntries)
FILE *fp;
int gametype, ntoks, nbuyers, nsellers, ntries;
{
int key,i,j,ok,loop,maxplayers;
int btoks[MAXTOKENS*MAXPLAYERS], stoks[MAXTOKENS*MAXPLAYERS], eqp, eqt;
char string[128];
ok = 1;
if (nsellers>nbuyers)
maxplayers = nsellers;
else
maxplayers = nbuyers;
for(i=0; ((i<ntries) && ok) ; i++)
{
for(j=0; ((j < ntoks*nbuyers) && ok) ; j++)
ok = (ok && (readint(fp,&btoks[j],0,9999) >= 0));
for(j=0; ((j < ntoks*nsellers) && ok) ; j++)
ok = (ok && (readint(fp,&stoks[j],0,9999) >= 0));
ok = (ok && (readint(fp, &eqp, 0, 9999) >= 0));
ok = (ok && (readint(fp, &eqt, 0, 9999) >= 0));
#ifdef LOGFILE
fprintf(logfil,"Line -> %d , Equil -> %d %d\n",i,eqp,eqt);
fflush(logfil);
#endif
printf("\n");
if (ok)
{
puts(" ");
puts("Sample Unit Values Information");
puts("===================");
puts("= Buyer = Seller =");
puts("===================");
for(loop=0; loop < ntoks*maxplayers; loop++)
{
if(loop<ntoks*nbuyers)
printf("= %4d =",btoks[loop]);
else
printf("= =");
if(loop<ntoks*nsellers)
printf(" %4d =\n",stoks[loop]);
else
printf(" =\n");
}
puts("===================");
printf("= Equil : %4d =\n",eqp);
printf("= Trades: %4d =\n",eqt);
puts("===================");
}
printf("\nWould you like to see another sample (y/n)? ");
gets(string);
key = string[0];
if(key == 'n' || key == 'N')
return;
}
}
/* This routines opens fp, it does not close fp! */
void open_intro_data()
{
history_data = (history_rec *)calloc(50,sizeof(history_rec));
if (!history_data) {
fprintf(stderr,"Calloc failed errno -> %d",errno);
exit(1);
}
if ((fp = fopen(DATA_FILE,"r")) == NULL) {
fprintf(stderr,"ERROR: cannot open file %s\n",DATA_FILE);
exit(1);
}
#ifdef LOGFILE
if ((logfil = fopen(LOG_FILE,"w")) < 0) {
fprintf(stderr,"ERROR: cannot open file %s\n",LOG_FILE);
exit(1);
}
#endif
if (readint(fp,&gametype,0,9999) < 0) {
fprintf(stderr,"ERROR: unable to read gametype from file %s\n",DATA_FILE);
exit(1);
}
if (readint(fp,&ntoks,0,MAXTOKENS) < 0) {
fprintf(stderr,"ERROR: unable to read ntoks from file %s\n",DATA_FILE);
exit(1);
}
if (ntoks > MAXTOKENS) {
fprintf(stderr,"ERROR: ntokens > MAXTOKENS in data file %s\n",DATA_FILE);
exit(1);
}
if (readint(fp,&nbuyers,0,MAXPLAYERS) < 0) {
fprintf(stderr,"ERROR: unable to read nbuyers from file %s\n",DATA_FILE);
exit(1);
}
if (nbuyers > MAXPLAYERS) {
fprintf(stderr,"ERROR: nbuyers > MAXPLAYERS in data file %s\n",DATA_FILE);
exit(1);
}
if (readint(fp,&nsellers,0,MAXPLAYERS) < 0) {
fprintf(stderr,"ERROR: unable to read nsellers from file %s\n",DATA_FILE);
exit(1);
}
if (nsellers > MAXPLAYERS) {
fprintf(stderr,"ERROR: nsellers > MAXPLAYERS in data file %s\n",DATA_FILE);
exit(1);
}
if (readint(fp,&ntries,0,9999) < 0) {
fprintf(stderr,"ERROR: unable to read ntries from file %s\n",DATA_FILE);
exit(1);
}
if (ntries > MAXTRIES) {
fprintf(stderr,"ERROR: ntries > MAXTRIES in data file %s\n",DATA_FILE);
exit(1);
}
} /* open_intro_data */
void intro_chart()
{
int oldcolor;
char string[128];
open_intro_data();
puts("The table below shows sample buyers' and sellers' units. The");
puts("approximate equilibrium value will also be displayed. These values");
puts("were chosen in the same random way as the values of the units in the");
puts("experiment you will be playing.");
printf("\nPress Enter/Return to continue...\n");
gets(string);
fflush(stdin);
show_supply_demand(fp,gametype,ntoks,nbuyers,nsellers,ntries);
fclose(fp);
free(history_data);
fflush(stdin);
} /* intro_chart */
int readint(fp,variable,minval,maxval)
FILE *fp;
int *variable;
int minval,maxval;
{
char buf[10],*ptr,c;
int answer,status;
status = gettok(fp,buf,10);
if (status==0) {
ptr = buf;
while ((c= *ptr++) != '\0' && isdigit(c));
answer = atoi(buf);
if(c!='\0')
return -1;
else {
if (answer<minval)
return -1;
else if (answer>maxval)
return -1;
else {
*variable = answer;
return answer;
}
}
}
else return -1;
} /* readint */
int gettok(fp,string,len)
FILE *fp;
char *string;
int len;
{
int c;
if (len<=0) return 1;
while ((c=getc(fp))!=EOF) {
if (c == '#')
while ((c=getc(fp))!=EOF && c!='\n') ;
if (!isspace(c))
break;
}
if (c==EOF)
return -1;
if (--len>0) *string++ = (char)c;
while ((c=getc(fp))!=EOF && !isspace(c))
if (--len>0) *string++ = (char)c;
if (c!=EOF)
ungetc(c,fp);
*string = '\0';
return (len<=0);
} /* gettok */